/** 创建页签 */
(function($){
	$.fn.tabs = function(options){
		var totalWidth = options.tabsWidth;
		var iframeId = options.iframeId;
		var liTabsId = options.liTabsId;
		var tabWidth = options.tabWidth + options.marginWidth;
		var homeUrl = options.homeUrl;
		var indexSum = 1;
		this.add = function(opt){
			$(".close_btn").click();
			$("#"+iframeId+" iframe").hide();
			if($("#li_"+opt.frameId).length>0){
				$("#li_"+opt.frameId).click();
				return;
			}
			indexSum = indexSum + 1;
			new $.Tab($("#"+liTabsId),{frameId:opt.frameId,url:opt.url,tabTitle:opt.tabTitle,isHome:opt.isHome});
			
		};
		
		$.Tab = function(elm, opt){
			var closeBtn = $("<span>",{
				"class":"btn_on",
				click:function(e){
					e.stopPropagation();
					$pNode = $(this).parent();
					$("#"+$pNode.attr("id")+" div").hasClass("tab_on")&&$("#"+liTabsId+" li").eq($pNode.index()-1).click();
					$("#"+$pNode.attr("targetFrame")).remove();
					$pNode.remove();
					indexSum = indexSum -1;
					var n = Math.floor(totalWidth/tabWidth);
					var dc = $("#"+liTabsId);
					if(indexSum >= n){
						dc.css({"margin-left":((n-indexSum)*tabWidth)});
					}else{
						dc.css({"margin-left":0});
					}
				}
			});
			var $tab = this.init(opt);
			opt.isHome||$tab.append(closeBtn);
			$(".tab_on").removeClass("tab_on").addClass("tab_off");
			$(".btn_on").removeClass("btn_on").addClass("btn_off");
			$(elm).append($tab);
		};
		
		$.Tab.prototype = {
			init:function(opt){
				$("<iframe>",{
					id:opt.frameId,
					src:opt.url,
					width:"100%",
					height:"100%",
					frameBorder:"0",
					"scrolling":"no"
				}).appendTo("#"+iframeId);
				
				var n = Math.floor(totalWidth/tabWidth);
				var dc = $("#"+liTabsId);
				_index = indexSum;
				if(indexSum > n){
					dc.css({"margin-left":((n-indexSum)*tabWidth)});
				}
				var strTitle = opt.tabTitle;
				if (strTitle.length >5 ){
					strTitle = strTitle.substring(0, 5)+"..";
				}
							
				return $("<li>",{
					id:"li_"+opt.frameId,
					"targetFrame":opt.frameId,
					"targetUrl":opt.url,
					"title":opt.tabTitle,
					html:'<div class="tab_on" title="'+opt.tabTitle +'">'+strTitle+'</div>',
					click:this.click
				});
			},
			click:function(){
				var $tab = $(this);
				$(".tab_on").removeClass("tab_on").addClass("tab_off");
				$(".btn_on").removeClass("btn_on").addClass("btn_off");
				$("#"+iframeId+" iframe").hide();
				$("#"+$tab.attr("targetFrame")).show();
				$("#"+$tab.attr("id")+" div").removeClass("tab_off").addClass("tab_on");
				$("#"+$tab.attr("id")+" span").removeClass("btn_off").addClass("btn_on");
				var n = Math.floor(totalWidth/tabWidth);
				var dc = $("#"+liTabsId);
				var t = $tab.index()+1;
				_index = t;
				if(t > n){
					dc.css({"margin-left":((n-t)*tabWidth)});
				}else{
					dc.css({"margin-left":0});
				}
			},
			refresh:function(){
				
			}
		};
		
		this.actFrameId = "";
		var toolBar = function(){
			var $tb = $("<div>",{id:"jTabs_Tool_bar", "class":"tab_btn_x"});	
			$tb.append($("<ul>",{id:"jTabs_down_menu", "class":"tab_x_list"})).click(function(){
				$("#jTabs_down_menu").toggle().html("");
				$("#"+liTabsId+" li").each(function(){
					var $li = $(this);
					var div = $("<li>",{
						"url" : $li.attr("targetUrl"),
						"tId" : $li.attr("id"),
						html  : "<a href='#'>"+$li.attr("title")+"</a>",
						click : function(){$("#"+$(this).attr("tId")).click();}
					});
					$("#jTabs_down_menu").append(div);
				});
			});
			return $tb;
		};
		
		var _index = 0;
		var arrowLeft = function(){
			return $("<div>",{
				id:"jTabs_left_btn", 
				"class":"tab_btn_l",
				click:function(){
					var n = Math.floor(totalWidth/tabWidth);
					if(_index < n && indexSum > n){
						_index = n;
					}
					if(indexSum <= n){
						return;
					}else if(_index == 0){
						_index = 0;
					}else{
						_index = _index - 1;
					}
					if(_index < n){
						$("#"+liTabsId).css({"margin-left":0});
					}else{
						$("#"+liTabsId).css({"margin-left":((n-_index)*tabWidth)});
					}
				}
			});
		}
		var arrowRight = function(){
			return $("<div>",{
				id:"jTabs_right_btn", 
				"class":"tab_btn_r",
				click:function(){
					var n = Math.floor(totalWidth/tabWidth);
					if(_index < n && indexSum > n){
						_index = n;
					}
					if(indexSum <= n){
						return;
					}else if(_index >= indexSum){
						_index = indexSum;
					}else{
						_index = _index + 1;
					}
					if(_index < n){
						$("#"+liTabsId).css({"margin-left":0});
					}else{
						$("#"+liTabsId).css({"margin-left":((n-_index)*tabWidth)});
					}
				}
			});
		}
		
		var arrowBar = function(){
			return  $("<div>",{"class":"header_tab_r"}).append(arrowLeft()).append(arrowRight()).append(toolBar());
		}
		
		return this.each(function(){
			
			var $ul = $("<ul>",{id:liTabsId, "class":"tab_cnt_cnt"});
			new $.Tab($ul,{isHome:true, tabTitle:"首页", frameId:'homePage0', url:homeUrl});
			var $div = $("<div>",{"class":"header_tab_l", width:totalWidth+"px"}).append($ul);
			$(this).append($div).append(arrowBar());
			$("#jTabs_down_menu").hover(
				function(){
					//$_swin.animate().stop(false,true);
				},
				function(){
					$("#jTabs_down_menu").hide();
				}
			);
		});
	};
})(jQuery);
